import Toast from '@vant/weapp/toast/toast'
import { login } from '../../request/api'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    addInfoShow: false,
    avatarUrl: 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0',
    avatar: wx.getStorageSync('avatar') ? wx.getStorageSync('avatar') : '',
    name: wx.getStorageSync('name'),
    loading: false,
    token: wx.getStorageSync('token')
  },
  // chackeLogin() {
  //   if (!wx.getStorageSync('token')) {
  //     wx.showToast({
  //       title: '您还未进行登录，请登录！',
  //       icon: 'error',
  //       duration: 2000
  //     })
  //   }
  // },
  openAddInfo() {
    this.setData({
      addInfoShow: true
    })
  },
  hideAddInfo() {
    this.setData({
      addInfoShow: false,
      loading: false
    })
  },
  onChooseAvatar(e) {
    const {
      avatarUrl
    } = e.detail
    this.setData({
      avatarUrl,
    })
    wx.setStorageSync('avatar', e.detail)
  },
  subminInfo(e) {
    wx.login({
      success: async(result) => {
        let data = {
          code: result.code
        }
        let that = this
        that.setData({
          loading: true
        })
        const res = await login(data)
        if (res.data.code == 200) {
          Toast({
            type: 'success',
            message: '登录成功',
            duration: 3000
          })
          console.log('登录', res)
          wx.setStorageSync('token', res.data.data.token)
          wx.setStorageSync('name', e.detail.value.name)
          that.setData({
            // name: wx.getStorageSync('name'),
            loading: false,
            addInfoShow: false,
            // token: wx.getStorageSync('token')
          })
        } else {
          Toast({
            type: 'error',
            duration: 3000,
            message: '网络繁忙，请稍后重试~'
          })
        }
      },
    })
  },
  userLogout() {
    wx.showModal({
      title: '提示',
      content: '确定要退出登录吗？',
      complete: (res) => {
        if (res.cancel) {
        }
    
        if (res.confirm) {
          Toast({
            type: 'success',
            message: '退出成功',
            duration: 3000
          })
          wx.removeStorageSync('token')
          this.setData({
            token: '',
            name: ''
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // this.chackeLogin()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})